package com.hutianao.gqgl.menu.service.impl;

import com.hutianao.gqgl.menu.respository.MenuDao;
import com.hutianao.gqgl.menu.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hutianao.gqgl.mybatis.ztree.ZtreeEntity;
import com.hutianao.gqgl.spring.control.BaseController;
import com.hutianao.gqgl.mybatis.entity.PtMenuEntity;
import com.hutianao.gqgl.spring.tag.entity.DictTagEntity;
import com.hutianao.gqgl.mybatis.flexigrid.PaddingEntity;
import com.hutianao.gqgl.mybatis.flexigrid.PadingRstType;
import com.hutianao.gqgl.mybatis.entity.PtMenuI18n;
import org.springframework.beans.factory.annotation.Value;

import java.util.List;

@Service
public class MenuServiceImpl extends BaseController implements MenuService {
    @Autowired
    private MenuDao menuDao;
    @Value("${unbound.id}")
    private String unboundId;
    @Override
    public List<ZtreeEntity> queryLeftTree() {
        List<ZtreeEntity> list = menuDao.queryLeftTree();
        list.add(getNoBoundNode());
        return list;
    }

    @Override
    public PtMenuEntity queryMenuListById(String menuId) {
        return menuDao.queryMenuListById(menuId);
    }

    @Override
    public List<DictTagEntity> queryMenuList() {
        return menuDao.queryMenuList();
    }
    @Override
    public PadingRstType<PtMenuEntity> queryMenuInfoListById(String menuId, PaddingEntity padding) {
        padding.deal(PtMenuEntity.class);
        PadingRstType<PtMenuEntity> rstType = new PadingRstType<>();
        rstType.setPage(padding.getPage());
        List<PtMenuEntity> list = menuDao.queryMenuInfoListPaddingById(menuId,padding);
        rstType.setRawRecords(list);
        Integer total =  menuDao.queryMenuInfoListTotal(menuId);;
        rstType.setTotal(total);
        rstType.putItems();

        return rstType;
    }

    @Override
    public List<PtMenuI18n> getI18nByMenuId(String menuId) {

        return  menuDao.getI18nByMenuId(menuId);
    }
    @Override
    public void deleteMenuI18nById(String menuId, String langId) {
        menuDao.deleteMenuI18nById(menuId,langId);
    }

    @Override
    public void saveMenuI18n(PtMenuI18n ptMenuI18n) {
        List<PtMenuI18n> list = menuDao.queryMenuI18n(ptMenuI18n);
        if(list != null && list.size() > 0){
            menuDao.updateMenuI18n(ptMenuI18n);
        }else {
            menuDao.insertMenuI18n(ptMenuI18n);
        }
    }
    @Override
    public List<ZtreeEntity> queryBoundMenuTree(String menuId) {
        List<ZtreeEntity> list = menuDao.queryBoundMenuTree();
        String parentId = menuDao.queryParentId(menuId);
        for(ZtreeEntity item:list){
            if(parentId.equals(item.getId())){
                item.setChecked(true);
                break;
            }
        }
        return list;
    }

    @Override
    public void saveParentIdByMenuId(String menuId, String parentId) {
        menuDao.saveParentIdByMenuId(menuId,parentId);
    }

    @Override
    public void unboundParentIdByMenuId(String menuId) {
        menuDao.saveParentIdByMenuId(menuId,unboundId);
    }


    @Override
    public void addMenuInfo(PtMenuEntity ptMenuEntity) {
        ptMenuEntity.setMenuId(String.valueOf(System.currentTimeMillis()));
        menuDao.addMenuInfo(ptMenuEntity);
    }

    @Override
    public void updateMenuInfo(PtMenuEntity ptMenuEntity) {
        menuDao.updateMenuInfo(ptMenuEntity);
    }

    @Override
    public void deleteMenuInfoByMenuId(String menuId) {
        menuDao.deleteMenuInfoByMenuId(menuId);
    }
}